<?php 
class ModelPhoneRate extends Model {
	public function setRate($data = array()) {
		$query = $this->db->query("SELECT phone_comment_id from phone_comments where phone_comment_id = '".(int)$data['comment_id']."'");
		
		if($query->row['phone_comment_id'] <= 0){
			return false;
		}
		else{
			$ip = $_SERVER['REMOTE_ADDR'];
			$query = $this->db->query("SELECT phone_rate_id from phone_rates where ip = '".$ip."' and phone_comment_id = '".(int)$data['comment_id']."'");
			if( isset($query->row['phone_rate_id']) && $query->row['phone_rate_id'] != ''){
				return false;
			}
			else{
				if((int)$data['rate'] == 1){
					$rate = +1;
					$comment_rate = 'rate + 1';
				}else{
					$rate = -1;
					$comment_rate = 'rate - 1';
				}

				$this->db->query("Insert into phone_rates SET phone_comment_id = '" . (int)$data['comment_id'] . "', rate = '" . $rate . "', ip = '" . $ip . "', created = '".date('Y-m-d',time())."'");
				$this->db->query("Update phone_comments Set rate = ". $comment_rate." Where phone_comment_id = '" .(int)$data['comment_id']. "'");
				
				return true;
			}
		}
	}
}
?>